<?php
namespace Android\Model;

use Think\Model;

class LifeModel extends Model
{


    //把图片保存到数据库
    // 数据添加到数据库中之后,$data['id']就是新添加的记录的ID
    protected function _after_insert($data, $option)
    {
        /**************************** 处理商品相册 **************************************/
        if (hasImage('pic')) {
            // 重新处理数组
            foreach ($_FILES['pic']['name'] as $k => $v) {
                if (empty($v))
                    continue;
                $pics[] = array(
                    'name' => $v,
                    'type' => $_FILES['pic']['type'][$k],
                    'tmp_name' => $_FILES['pic']['tmp_name'][$k],
                    'error' => $_FILES['pic']['error'][$k],
                    'size' => $_FILES['pic']['size'][$k],
                );
            }
            $_FILES = $pics;  // uploadOne函数中会到$_FILES数组中找图片，所以把处理好的图片信息传到这里
            // 循环这个数组然后一个一个上传并生成缩略图
            $gpModel = D('life_files');
            foreach ($pics as $k => $v) {
                $ret = uploadOne($k, 'Life');
                $sub = $ret['images'][0];
                $str = substr(strrchr($sub, '.'), 1);
                if (in_array("$str", array('mp4', 'rmvb', 'mkv', 'avi'))) {
                    $gpModel->add(array(
                        'life_id' => $data['life_id'],
                        'video' => $ret['images'][0],
                    ));
                } else {
                    $gpModel->add(array(
                        'life_id' => $data['life_id'],
                        'image' => $ret['images'][0],
                    ));
                }

            }
        }

    }


    //好友动态
    public function lifes(&$user)
    {
        $friends = D('friends');
        $info = [];
        //获取该好友所有的好友
        $user_id = $friends->join('user on friends.friend_id=user.user_id')->field('user.user_id')->where('type=2 and friends.user_id=' . $user)->select();
        $life_reply = D('life_reply');
        $user = D('user');
        foreach ($user_id as $k => $v) {
            $user_id = $v['user_id'];
            //发布的内容
            $name = $v['nickname'];
            //发布动态里面有该用户
            $info[$k]['pub'] = $this->where("user_id=" . $user_id)->select();
            $pub = $info[$k]['pub'];
            //如果该好友发布了则查询用户信息
            if (!empty($pub)) {
                foreach ($pub as $key1 => $val1) {
                    $user_id = $val1['user_id'];
                    $info[$k]['pub'][$key1]['user'] = $user->field('nickname,head_img')->where('user_id=' . $user_id)->find();
                }
                //获取点赞的人
                //获得该用户发布的活动 key2 发布的
                foreach ($pub as $key2 => $value2) {
                    $life_id = $pub[$key2]['life_id'];
                    $assist = $this->alias('li')->join('life_assist ass on li.life_id=ass.life_id')->join('user on user.user_id=assist_user')->field('user.user_name,user.user_id')->where('li.life_id=' . $life_id)->select();
                    $info[$k]['pub'][$key2]['assist'] = $assist;
                    //获取发布的视频图片
                    $video = $this->join('life_files as fl on  life.life_id=fl.life_id')->field('video')->where('fl.life_id=' . $life_id)->find();
                    $image = $this->join('life_files as fl on  life.life_id=fl.life_id')->field('image')->where('fl.life_id=' . $life_id)->select();
                    if ($video) {
                        $info[$k]['pub'][$key2]['video'] = $video;
                    }
                    if ($image) {
                        $info[$k]['pub'][$key2]['image'] = $image;
                    }
                    $z = $info[$k]['pub'][$key2]['image'];
                    //回复的
                    $user_id = $value2['user_id'];
                    $life_id = $value2['life_id'];
                    //得到回复
                    $info[$k]['pub'][$key2]['relay'] = $life_reply->alias(re)->join('life li on li.life_id=re.trends_id')->join('user us on us.user_id=re.user_id')->field('us.user_id,nickname,head_img,re.time2,re.id,re.trends_id,re.user_id,re.time2,reply_content,parent_id')->where("parent_id=0 and trends_id=" . $life_id)->select();
                    $reply_again = $info[$k]['pub'][$key2]['relay'];
                    //二层回复推后
                    //根据父类的id 得到回复的id
//                        foreach ($reply_again as $ke => $va) {
//                            $parent_id = $va['trends_id'];
//                            $user_id = $va['user_id'];
//                            $info[$k]['pub'][$key2]['relay'][$ke]['again'] = $life_reply->join('user us on us.user_id= life_reply.user_id')->field('reply_content,time2,us.user_id,us.user_id,to_userid,nickname,trends_id,head_img')->where("life_reply.user_id=" . $user_id." and trends_id=".$parent_id )->select();
//                            //查询给谁回复的用户的信息
//                             $again=$info[$k]['pub'][$key2]['relay'][$ke]['again'];
//                             foreach ($again as $key4=>$value4){
//                                 $touser_id=$value4['to_userid'];
//                                 $trends_id=$value4['trends_id'];
//                                 if($touser_id!=0){

                    //原来写的
//                                     $info[$k]['pub'][$key2]['relay'][$ke]['again'][$key4]['to_user'] = $life_reply->join('user us on us.user_id= life_reply.to_userid')->field('to_userid,nickname,head_img')->where("to_userid=" . $touser_id ." and trends_id=".$trends_id." and life_reply.user_id=".$touser_id)->select();
                    //李善辉
                    $sql = "SELECT life_reply.*, us.`nickname`,us.`head_img`,us2.`nickname` AS  tonickname,us2.`head_img` AS tohead_img
FROM `life_reply` 
INNER JOIN USER  AS us ON us.user_id= life_reply.to_userid 
LEFT JOIN  USER AS us2 ON us2.user_id= life_reply.user_id
 WHERE(trends_id=$life_id) 
 ";
                    $Model = M();
                    $z = $Model->query($sql);
//                    $info[$k]['pub'][$key2]['relay'][] = $life_reply->join('user us on us.user_id= life_reply.to_userid')->join('AS us2 ON us2.user_id= life_reply.user_id', 'left')->field('us.`nickname`,us.`head_img`,us2.`nickname` AS  tonickname,us2.`head_img` AS tohead_img')->where("trends_id=" . $life_id)->select();
//                                                 echo $life_reply->getLastSql();
//
//                                     echo $life_reply->getLastSql();
//                                 }
//                            }
//                        }
                    $info[$k]['pub'][$key2]['relay'] = $z;
                    $time[] = $info[$k]['pub'][$key2];

                }
            }

        }
        rsort($time);
        return $time;
    }


    //好友动态
    public function lifescopy(&$user)
    {
        $friends = D('friends');
        $info = [];
        //获取用户的id
        $user_id = $friends->join('user on friends.friend_id=user.user_id')->field('user.user_id,nickname,head_img')->where('type=2 and friends.user_id=' . $user)->select();
        $life_reply = D('life_reply');
        foreach ($user_id as $k => $v) {
            $user_id = $v['user_id'];
            //发布的内容
            $name = $v['nickname'];
            $info[$name]['pub'] = $this->where("user_id=" . $user_id)->select();
            $pub = $info[$name]['pub'];
            //获取点赞的人
            $life_id = $pub[$name]['life_id'];
            $assist = $this->alias('li')->join('life_assist ass on li.life_id=ass.life_id')->join('user on user.user_id=assist_user')->field('user.user_name')->where('li.life_id=' . $life_id)->select();
            $info[$name]['pub'][$k]['assist'] = $assist;
            foreach ($pub as $key => $value) {
                //回复的
                $user_id = $value['user_id'];
                $life_id = $value['life_id'];
                //得到回复
                $info[$name]['pub'][$key]['relay'] = $life_reply->alias(re)->join('life li on li.life_id=re.trends_id')->join('user us on us.user_id=re.user_id')->field('us.user_id,nickname,head_img,re.time2,re.id,re.trends_id,re.user_id,re.time2,reply_content,parent_id')->where("parent_id=0 and trends_id=" . $life_id)->select();
                $reply_again = $info[$name]['pub'][$key]['relay'];
                //二层回复推后
                //根据父类的id 得到回复的id
                foreach ($reply_again as $ke => $va) {
                    $parent_id = $va['trends_id'];
                    $user_id = $va['user_id'];
                    $info[$name]['pub'][$key]['relay'][$ke]['again'] = $life_reply->join('user us on us.user_id= life_reply.user_id')->field('reply_content,time2,us.user_id,us.user_id,nickname,head_img')->where("parent_id=" . $parent_id)->select();

                }
            }

        }
        return $info;
    }

}